﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace NetTrafficStatistics
{
	public partial class MainForm : Form
	{
		private readonly string LabelInput, LabelOutput, LabelGen, LabelAllyears;
		private int Counter;

		public MainForm ()
		{
			InitializeComponent ();

			// Init values
			LabelInput = label5.Text;
			LabelOutput = label7.Text;
			LabelGen = label9.Text;
			LabelAllyears = label3.Text;

			listBoxMenu1.SelectedIndex = listBoxMenu2.SelectedIndex = 0;
			listBoxMenu3.SelectedIndex = 2;
		}

		private void label14_Click (object sender, EventArgs e)
		{
			// Export
			Hide();

			var sfd = new SaveFileDialog {FileName = "traffic.csv", Filter = "*.csv files|*.csv|All files|*.*"};
			if (sfd.ShowDialog() == DialogResult.OK)
			{
				File.WriteAllText (sfd.FileName, SqLiteDb.ExportTableCsv());
			}
			sfd.Dispose();
		}

		private void label4_Click (object sender, EventArgs e)
		{
			// Reset
		}

		private void exitToolStripMenuItem_Click (object sender, EventArgs e)
		{
			Hide();
			Application.Exit ();
		}

		private void Form1_FormClosing (object sender, FormClosingEventArgs e)
		{
			if (e.CloseReason == CloseReason.UserClosing)
			{
				Hide();
				e.Cancel = true;
			}
		}

		private void MainForm_Move (object sender, EventArgs e)
		{
			dataPanel1.Refresh ();
		}

		private void Form1_KeyDown (object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
				Hide ();

			e.Handled = false;
		}

		private void ShowTable (object sender, EventArgs w)
		{
			var p = new Params (listBoxMenu1.SelectedIndex, listBoxMenu3.SelectedIndex + 1);
			var u = Tools.GetUnits (listBoxMenu2.SelectedIndex);
			label5.Text = String.Format (LabelInput, u);
			label7.Text = String.Format (LabelOutput, u);
			label9.Text = String.Format (LabelGen, DateTime.Now.ToLongDateString () + " " + DateTime.Now.ToLongTimeString ());
			label6.Text = listBox1.Items.Count > listBoxMenu1.SelectedIndex ? listBox1.Items[listBoxMenu1.SelectedIndex].ToString () : "";
			AddLabelPath (p.Path, p.Link);

			if (dataPanel1.ShowTable (p, u))
			{
				label10.Visible = false;
			}
			else
			{
				label10.Visible = true;
				dataPanel1.Height = 150;
			}
			dataPanel1.Refresh ();
			WindowState = FormWindowState.Normal;
			Show ();
		}

		private void AddLabelPath (string[] list, int[] links)
		{
			panel1.Controls.Clear ();
			var pos = AddLabel (LabelAllyears, 8, 0);		// TODO: 8 = listBoxMenu1 'All years' position

			if (list.Length > 0)
			{
				for (var i = 0; i < list.Length - 1; i++)
				{
					pos = AddLabel (">", null, pos);
					pos = AddLabel (list[i], links != null ? links[i] : (int?) null, pos);
				}
				pos = AddLabel (">", null, pos);
				AddLabel (list[list.Length - 1], null, pos);
			}
		}

		private int AddLabel (string text, int? link, int left)
		{
			var f = link != null ? Tools.Fbu : Tools.Fb;
			var g = Graphics.FromHwnd (Handle);
			var size = g.MeasureString (text, f);
			Control l = new TransparentLabel
			{
				Text = text,
				Font = f,
				AutoSize = true,
				BackColor = Color.Transparent,
				Left = left
			};

			if (link != null)
			{
				l.Cursor = Cursors.Hand;
				l.Click += (o, e) =>
				{
					listBoxMenu1.SelectedIndex = (int) link;
					ShowTable (null, null);
				};
			}

			panel1.Controls.Add (l);
			return (int) (left + size.Width + 2);
		}

		private void timer1_Tick (object sender, EventArgs e)
		{
			var p = Tools.Counter;
			label13.Text = (p - Counter).ToString();
			Counter = p;
		}
	}
}
